<html>
<body>
<head>
<title></title>
<script type ="text/javascript">
var groups = 1;
function add_textbox(){
	
	groups++;
	tester = "";
	var myString = "";
	
	for(i = 1; i < groups; i++){
		value = i+1;
		//alert(value);
		test = "group"+value;
		if (i == groups-1){
			tester = "";
		}
		else{
			
			tester = document.getElementById(test).value;
			
		}
		//alert(tester);
		myString += "Group "+value+" <input id ="+test+" type='text' name='group"+value+"' value="+tester+">"+"<br>";
		//alert(tester);
	}
	document.getElementById("optional_groups").innerHTML = myString;
}
function delete_textbox(){
	
	if(groups == 1){
	}
	else{
		groups--;
	}
	var myString = "";
	for(i = 1; i < groups; i++){
		value = i+1;

		test = "group"+value;

		tester = document.getElementById(test).value;
			
		
		myString += "Group "+value+" <input id ="+test+" type='text' name='group"+value+"' value="+tester+">"+"<br>";
		
	}
	document.getElementById("optional_groups").innerHTML = myString;	
	
}






function convert(month){
	
	if (month == "jan"){
		return 1;
	}
	else if (month == "feb"){
		return 2;
	}
	else if (month == "mar"){
		return 3;
	}
	else if(month == "april"){
		return 4;
	}
	else if(month == "may"){
		return 5;
	}
	else if(month == "june"){
		return 6;
	}
	else if(month == "july"){
		return 7;
	}
	else if(month == "august"){
		return 8;
	}
	else if(month == "sept"){
		return 9;
	}
	else if(month == "oct"){
		return 10;
	}
	else if(month == "nov"){
		return 11;
	}
	else if (month == "dec"){
		return 12;
	}
}

	function check(test,test2){
	
		var e = document.getElementById(test);
		var month = e.options[e.selectedIndex].value;
		//document.write(month);
		day = document.getElementById(test2);
		limit = day.options.length;
		if (month == "feb"){
			if (limit == 29){
				//do nothing
			}
			else{
				/*
				for(i = 28; i <= limit; i++){
					optn = document.createElement("OPTION");
					option.text = i;
					option.value = i;
					day.options.add(optn);
				}*/
				while (limit != 29){
					day.remove(limit-1);
					limit =  day.options.length;
					//alert(limit);
				}
			}
		}
		else if (month == "jan" || month == "mar" || month == "may" || month == "july" || month == "august" || month == "oct" || month == "dec"){
			if (limit == 31){//31 days
			}
			else {
				while(limit < 31){
					//alert(limit);
					optn = document.createElement("option");
					optn.text = limit+1;
					optn.value = limit+1;
					day.options.add(optn);
					limit = day.options.length;
				}
			}
		}
		else{
			if (limit == 30){
			}
			else if (limit > 30){
				day.remove(30);
			}
			else{
				while(limit < 30){
					//alert(limit);
					optn = document.createElement("option");
					optn.text = limit+1;
					optn.value = limit+1;
					day.options.add(optn);
					limit = day.options.length;			
				}
			}
		}
	}
function valid_date(){
	bm = document.getElementById("try");
	bd = document.getElementById("try2");
	by = document.getElementById("start_year");
	var begin_month = bm.options[bm.selectedIndex].value;
	var begin_day = bd.options[bd.selectedIndex].value;
	var begin_year = by.options[by.selectedIndex].value;
	result2 = true;
	begin_month = convert(begin_month);
	begin_day = Number(begin_day);
	begin_year = Number(begin_year);
	em = document.getElementById("end_try");
	ed = document.getElementById("end_try2");
	ey = document.getElementById("end_year");
	
	var end_month = em.options[em.selectedIndex].value;
	var end_day = ed.options[ed.selectedIndex].value;
	var end_year = ey.options[ey.selectedIndex].value;
	end_month = convert(end_month);
	end_day = Number(end_day);
	end_year = Number(end_year);
	
	//alert(begin_month);
	//alert(begin_day);
	//alert(begin_year);
	result = false;
	//alert(end_month);
	//alert(end_day);
	//alert(end_year);
	if (end_year == begin_year){
		if (end_month == begin_month){
			if (end_day >= begin_day){
				result = true;
			}
			else {
				
			}
		}
		else if (end_month < begin_month){
			
		}
		else{
			result = true;
		}
	}
	else if (end_year < begin_year){
		
	}
	else{
		result = true;
	}
	
	//id_index = 1;
	//alert("wooot");
	//alert(document.getElementById("group1").value);
	//alert(groups);
	check = document.getElementById("use_groups").checked;
	if (check == 1){
		for(i = 1; i <= groups; i++){
			id_ind = "group"+i;
			
			toCheck = document.getElementById(id_ind).value;
			//alert(toCheck);
			if(toCheck == null || toCheck == ""){
				result2 = false;
				break;
			}
			
		}
		
		
		if (result == true && result2 == true){
			return true;
		}
		else {
			if(result == false && result2 == true){
				alert('Invalid begin date -> end date combination');
			}
			else if (result2 == false && result == false) {
				alert('Invalid begin date -> end date combination');
				alert('groups must be filled out!');
			}
			else if (result2 == false && result == true){
				alert('groups must be filled out!');
			}
			return false;
		}	
	}
	else{
		result2 = true;
		if (result == true && result2 == true){
			return true;
		}
		else {
			if(result == false && result2 == true){
				alert('Invalid begin date -> end date combination');
			}
			else if (result2 == false && result == false) {
				alert('Invalid begin date -> end date combination');
				alert('groups must be filled out!');
			}
			else if (result2 == false && result == true){
				alert('groups must be filled out!');
			}
			return false;
		}	
	}

}
function over_all_check(){
	result = valid_date();
	valid = true;
	survey_name = document.getElementById("survey_name").value;
	if (survey_name == "" || survey_name == null){
		//alert("Survey name must be filled out!")
		valid = false;
	}
	if (valid == false){
	
		alert("Survey name must be filled out!");
		return false;
	}
	
	else{

		return result;
	}
	
	
}

	
</script>
</head>
<h2>Create a Survey</h2>
<form method="POST" action='http://localhost/pollwatcher/index.php/user/insert_survey_todb' onsubmit="return over_all_check()">

Survey name: <input id="survey_name" type="text" name="survey_name"/><br/>
Description: <br/><textarea name="description" cols=60 rows=6>Insert a mini description of your survey..</textarea><br/>
Begin date: Month:<select id = "try" name="month" onchange="check('try','try2')">
<option value="jan"
<?php
	if ($month_cur == 1){
		echo " SELECTED ";
	}
?>
>January</option>
<option value="feb"
<?php
	if ($month_cur == 2){
		echo " SELECTED ";
	}
?>
>February</option>
<option value="mar"
<?php
	if ($month_cur == 3){
		echo " SELECTED ";
	}
?>
>March</option>
<option value="april"
<?php
	if ($month_cur == 4){
		echo " SELECTED ";
	}
?>
>April</option>
<option value="may"
<?php
	if ($month_cur == 5){
		echo " SELECTED ";
	}
?>
>May</option>
<option value="june"
<?php
	if ($month_cur == 6){
		echo " SELECTED ";
	}
?>
>June</option>
<option value="july"
<?php
	if ($month_cur == 7){
		echo " SELECTED ";
	}
?>
>July</option>
<option value="august"
<?php
	if ($month_cur == 8){
		echo " SELECTED ";
	}
?>
>August</option>
<option value="sept"
<?php
	if ($month_cur == 9){
		echo " SELECTED ";
	}
?>
>September</option>
<option value="oct"
<?php
	if ($month_cur == 10){
		echo " SELECTED ";
	}
?>
>October</option>
<option value="nov"
<?php
	if ($month_cur == 11){
		echo " SELECTED ";
	}
?>
>November</option>
<option value="dec"
<?php
	if ($month_cur == 12){
		echo " SELECTED ";
	}
?>
>December</option>
</select>

Day:<select id = "try2" name="day">
<?php
	if (in_array($month,array(1,3,5,7,8,10,12))){
		$N = 31;
	}
	else if ($month_cur == 2){
		$N = 28;
	}
	else{
		$N = 30;
	}
	
	for($i = 1; $i<= $N; $i++){
		if ($day_cur == $i){
			echo "<option value=\"$i\" SELECTED>";
		}
		else{
			echo "<option value=\"$i\">";
		}
		echo $i;
		echo "</option>";
	}
?>
</select>

Year<select id='start_year' name="year">
<?php
		$year = (int)date('Y');
		for($i = $year; $i<($year+100); $i++){
			//echo "<option "."value='$i'".">".$i."</option>";
			
			echo "<option ";
			echo "value=$i";
			echo ">";
			echo $i;
			echo "</option>";
		}
?>
</select><br/>

End date: Month:<select id = "end_try" name="end_month" onchange="check('end_try','end_try2')">
<option value="jan"
<?php
	if ($month_cur == 1){
		echo " SELECTED ";
	}
?>
>January</option>
<option value="feb"
<?php
	if ($month_cur == 2){
		echo " SELECTED ";
	}
?>
>February</option>
<option value="mar"
<?php
	if ($month_cur == 3){
		echo " SELECTED ";
	}
?>
>March</option>
<option value="april"
<?php
	if ($month_cur == 4){
		echo " SELECTED ";
	}
?>
>April</option>
<option value="may"
<?php
	if ($month_cur == 5){
		echo " SELECTED ";
	}
?>
>May</option>
<option value="june"
<?php
	if ($month_cur == 6){
		echo " SELECTED ";
	}
?>
>June</option>
<option value="july"
<?php
	if ($month_cur == 7){
		echo " SELECTED ";
	}
?>
>July</option>
<option value="august"
<?php
	if ($month_cur == 8){
		echo " SELECTED ";
	}
?>
>August</option>
<option value="sept"
<?php
	if ($month_cur == 9){
		echo " SELECTED ";
	}
?>
>September</option>
<option value="oct"
<?php
	if ($month_cur == 10){
		echo " SELECTED ";
	}
?>
>October</option>
<option value="nov"
<?php
	if ($month_cur == 11){
		echo " SELECTED ";
	}
?>
>November</option>
<option value="dec"
<?php
	if ($month_cur == 12){
		echo " SELECTED ";
	}
?>
>December</option>
</select>

Day:<select id = "end_try2" name="end_day">
<?php
	if (in_array($month,array(1,3,5,7,8,10,12))){
		$N = 31;
	}
	else if ($month_cur == 2){
		$N = 28;
	}
	else{
		$N = 30;
	}
	
	for($i = 1; $i<= $N; $i++){
		if ($day_cur == $i){
			echo "<option value=\"$i\" SELECTED>";
		}
		else{
			echo "<option value=\"$i\">";
		}
		echo $i;
		echo "</option>";
	}
?>
</select>

Year<select id ="end_year" name="end_year">
<?php
		$year = (int)date('Y');
		for($i = $year; $i<($year+100); $i++){
			//echo "<option "."value='$i'".">".$i."</option>";
			echo "<option ";
			echo "value=$i";
			echo ">";
			echo $i;
			echo "</option>";
		}
?>
</select><br/>
<input id="use_groups" name="checker" type="checkbox" CHECKED value="1"><label for="use_groups"> Use question groups</label>
<div id="groups">
	Group 1 <input id="group1"  type="text" name="group1"/>
	<div id ="optional_groups">
		
	</div>
	<button type="button" onclick="add_textbox()">Add Group</button>
	<button type="button" onclick="delete_textbox()">Delete Group</button>
</div>
<script type="text/javascript" src="<?php echo base_url(); ?>/public/js/jquery.js"></script>
	<script type="text/javascript">
	//alert("a");
    $(document).ready(function(){
       //$("#groups").css("display","none");
	   //alert("a");
       //$("#text_options_Text").css("display","none");
      // $("#choice_limit_div").css("display","none");
	 	$("#use_groups").click(function(){
			if($("#use_groups").is(":checked")){
				$("#groups").show("fast");
			}
			else{
				$("#groups").hide("fast");
			}
	   });
	   
	  
       }
       

    
    );
</script>
<br/>
Notification schedule:<select name="sched">

<option value="1">1 Day</option>
<option value="5">5 Days</option>
<option value="10">10 Days</option>
<option value="15">15 Days</option>
<option value="30">30 Days</option>
<option value="0">no notification</option>
</select>
<input type ="hidden" name="id" value = "<?php echo $id;?>"/><br/>

Notification message:<br />

<input type="radio" id="template" name="mode" value="template"> Use an existing template</input><br/>
<input type="radio" id="custom" name="mode" value="custom"> Use a custom message</input><br/><br/>

<?php echo form_textarea(array('id' => 'body', 'name' => 'notification_message', 'cols' => '70', 'rows' => '10', 'style' => 'display:none'), 'Insert a notification message'); ?>

<div id="loaded" style="display:none">
Templates: <br/>
<?php foreach($templates as $template)
{
	echo "<input type=\"radio\" name=\"loaded_templates\" value=\"$template\"> Use this template</input><br/><textarea readonly cols=70 rows=10> $template</textarea><br/><br/>";	
}

?>

</div>
<br /><br />

Required Information from Respondents: <br/>
<input type="checkbox" name="required[]" value="firstname" id="chkFirstName"><label for="chkFirstName"> FirstName</label><br/>
<input type="checkbox" name="required[]" value="middlename" id="chkMiddleName"><label for="chkMiddleName"> Middlename</label><br/>
<input type="checkbox" name="required[]" value="lastname" id="chkLastName"><label for="chkLastName"> Lastname</label><br/>
<input type="checkbox" name="required[]" value="gender" id="chkGender"><label for="chkGender"> Gender</label><br/>
<input type="checkbox" name="required[]" value="birthday" id="chkBirthday"><label for="chkBirthday"> Birthday</label><br/>
<input type="checkbox" name="required[]" value="location" id="chkLocation"><label for="chkLocation"> Location</label><br/><br>
<input type="submit" class = "reg" name="create" value="Create Survey!">

</body>
</html>

<script src="<?php echo base_url(); ?>public/js/jquery.js"></script>
<script type="text/javascript">
	
	$(document).ready(function(){
		$("#template").change(function(){
			$("#body").hide('fast');
			$("#loaded").show('fast');
			
		});
		
		$("#custom").change(function(){
			$("#loaded").hide('fast');
			$("#body").val('Insert a notification message');
			$("#body").show('fast');
		});
		
		$("input[name='loaded_templates']").change(function(){
				$("#body").val($("input[name='loaded_templates']:checked").val());
			});
	});
</script>
